package com.linxiao.hrcrm.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException;
import com.linxiao.hrcrm.domain.CrmCustomer;
import com.linxiao.hrcrm.domain.CrmProject;
import com.linxiao.hrcrm.domain.CrmWorksite;
import com.linxiao.hrcrm.service.ICrmCustomerService;
import com.linxiao.hrcrm.service.ICrmProjectService;
import com.linxiao.hrcrm.service.ICrmWorksiteService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.linxiao.hrcrm.mapper.CrmCustomerProjectMapper;
import com.linxiao.hrcrm.domain.CrmCustomerProject;
import com.linxiao.hrcrm.service.ICrmCustomerProjectService;

import javax.annotation.Resource;

/**
 * 客户项目关联Service业务层处理
 *
 * @author zhoudong
 * @date 2024-01-03
 */
@Service
public class CrmCustomerProjectServiceImpl implements ICrmCustomerProjectService {
    @Resource
    private CrmCustomerProjectMapper crmCustomerProjectMapper;
    @Resource
    private ICrmWorksiteService crmWorksiteService;
    @Resource
    private ICrmProjectService crmProjectService;
    @Resource
    private ICrmCustomerService crmCustomerService;

    /**
     * 查询客户项目关联
     *
     * @param id 客户项目关联主键
     * @return 客户项目关联
     */
    @Override
    public CrmCustomerProject selectCrmCustomerProjectById(Long id) {
        return crmCustomerProjectMapper.selectCrmCustomerProjectById(id);
    }

    /**
     * 查询客户项目关联列表
     *
     * @param crmCustomerProject 客户项目关联
     * @return 客户项目关联
     */
    @Override
    public List<CrmCustomerProject> selectCrmCustomerProjectList(CrmCustomerProject crmCustomerProject) {
        List<CrmCustomerProject> crmCustomerProjects = crmCustomerProjectMapper.selectCrmCustomerProjectList(crmCustomerProject);
        if (CollUtil.isNotEmpty(crmCustomerProjects)) {
            crmCustomerProjects.parallelStream().forEach(ccp -> {
                CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(ccp.getCustomerId());
                if (crmCustomer != null) {
                    ccp.setCustomerName(crmCustomer.getCustomerName());
                } else {
                    ccp.setCustomerName("未知");
                }
            });
        }
        return crmCustomerProjects;
    }

    /**
     * 新增客户项目关联
     *
     * @param crmCustomerProject 客户项目关联
     * @return 结果
     */
    @Override
    public int insertCrmCustomerProject(CrmCustomerProject crmCustomerProject) {
        // 查询用户是否在当前项目，如果已经存在关联，则不在继续保存
        CrmCustomerProject query = new CrmCustomerProject();
        query.setMchId(crmCustomerProject.getMchId());
        query.setCustomerId(crmCustomerProject.getCustomerId());
        query.setProjectId(crmCustomerProject.getProjectId());
        List<CrmCustomerProject> crmCustomerProjects = selectCrmCustomerProjectList(query);
        if (CollUtil.isNotEmpty(crmCustomerProjects)) {
            throw new UtilException("当前客户已经在当前项目存在");
        }

        checkParams(crmCustomerProject);
        crmCustomerProject.setCreateTime(DateUtils.getNowDate());
        return crmCustomerProjectMapper.insertCrmCustomerProject(crmCustomerProject);
    }

    /**
     * 参数检验
     * @param crmCustomerProject
     */
    private void checkParams(CrmCustomerProject crmCustomerProject) {
        // 查询项目
        CrmProject crmProject = crmProjectService.selectCrmProjectById(crmCustomerProject.getProjectId());
        if (crmProject == null) {
            throw new UtilException("项目有误");
        }
        // 查询工地
        CrmWorksite crmWorksite = crmWorksiteService.selectCrmWorksiteById(crmProject.getWorksiteId());
        if (crmWorksite == null) {
            throw new UtilException("工地有误");
        }

        // 项目负责人ID
        crmCustomerProject.setEmployeeId(crmProject.getEmployeeId());
        crmCustomerProject.setWorksiteId(crmWorksite.getId());
    }

    /**
     * 修改客户项目关联
     *
     * @param crmCustomerProject 客户项目关联
     * @return 结果
     */
    @Override
    public int updateCrmCustomerProject(CrmCustomerProject crmCustomerProject) {
        checkParams(crmCustomerProject);
        crmCustomerProject.setUpdateTime(DateUtils.getNowDate());
        return crmCustomerProjectMapper.updateCrmCustomerProject(crmCustomerProject);
    }

    /**
     * 批量删除客户项目关联
     *
     * @param ids 需要删除的客户项目关联主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerProjectByIds(Long[] ids) {
        return crmCustomerProjectMapper.deleteCrmCustomerProjectByIds(ids);
    }

    /**
     * 删除客户项目关联信息
     *
     * @param id 客户项目关联主键
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerProjectById(Long id) {
        return crmCustomerProjectMapper.deleteCrmCustomerProjectById(id);
    }
}
